package com.machine.filling.net.http.network;

import com.google.gson.Gson;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.modle.ClientInfoModle;
import com.machine.filling.net.ws.network.WsReqParam;
import com.machine.filling.util.Common;
import com.machine.filling.util.LogUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Callback;


/**
 * Created by LENOVO on 2017/12/16.
 */

public class APIHttpManager {
    private static APIHttpManager apiHttpManager;
    private Map<String, String> requestParams;

    private APIHttpManager() {

    }

    public static APIHttpManager getInstance() {
        if (apiHttpManager == null) {
            apiHttpManager = new APIHttpManager();
        }
        return apiHttpManager;
    }


    /**
     * 登陆
     *
     * @param userName 用户名
     * @param pwd      密码
     * @param deviceId 设备id 唯一标识
     * @param callback
     */
    public void login(String userName, String pwd, String deviceId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("userName", userName);
        requestParams.put("pwd", pwd);
        requestParams.put("deviceNum", deviceId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_LOGIN, requestParams, callback);
    }

    /**
     * 获取油站信息
     *
     * @param num      油站编号
     * @param callback
     */
    public void getOilStationInfo(String num, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("num", num);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_OIL_STATION_INFO, requestParams, callback);
    }

    /**
     * 获取油机列表
     *
     * @param sellerId 油站id
     * @param callback
     */
    public void getOilMachineList(String sellerId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId", sellerId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_OIL_MACHINE_LIST, requestParams, callback);
    }

    /**
     * 绑定终端机和油机
     *
     * @param stationId 油站id
     * @param machineId 油机id
     * @param clientId  设备id （接口返回）
     * @param callback
     */
    public void bindClientAndOilMachine(String stationId, String machineId, String clientId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId", stationId);
        requestParams.put("machineId", machineId);
        requestParams.put("clientId", clientId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_BIND_CLIENT_AND_OIL_MACHINE, requestParams, callback);
    }

    /**
     * 重置终端机
     *
     * @param stationId
     * @param machineId
     * @param clientId
     * @param callback
     */
    public void reSetOilClient(String stationId, String machineId, String clientId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId", stationId);
        requestParams.put("machineId", machineId);
        requestParams.put("clientId", clientId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_UNBIND_CLIENT_AND_OIL_MACHINE, requestParams, callback);
    }

    public void getTokenClient(Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId","13");
        requestParams.put("machineId", "13");
        requestParams.put("machineNum", "ae34572ff4a543b7");
        requestParams.put("deviceId", "13");
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_GET_TOKEN, requestParams, callback);
    }

    public void reSetOilEngineClient(Callback callback){
        ClientInfoModle modle = ClientInfoDao.getInstance().getModleByDeviceNum();
        LogUtil.e("deviceNum-->"+ modle.getDeviceNum());
        requestParams = new HashMap<>();
        requestParams.put("deviceNum",modle.getDeviceNum());
        requestParams.put("token","eyJhbGciOiJIUzUxMiIsInppcCI6IkRFRiJ9.eNocy10KAiEUQOG93GcZ8N_cQS9tYVC7Q4Y6Qyo2RHtPej2c7wMVU8LX9Q6WcgLZhUcseOsZLDjkQmq2bcJJwb0GAtG1OUolJFfMaAL4PsAyepHKmH-ItU46xlie0Z179y6UJex54pAilra288BpCNTu50o5fH8AAAD__w.96bVTi29pjRmb6pl7QNzucDv3oFd-Qfirx8G2wjtuc5XvtstzwWVz2WPR2_mB-SV4XvGsRZ4tIsJMxyXr6noKg");
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_UNBIND_CLIENT_AND_OIL_MACHINE_SECOND, requestParams, callback);
    }

    /**
     * 获取油品列表
     *
     * @param sellerId 油站id
     * @param callback
     */
    public void getOilTypeList(String sellerId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId", sellerId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_OIL_TYPE_LIST, requestParams, callback);
    }

    /**
     * 获取油罐列表
     *
     * @param sellerId 油站id
     * @param callback
     */
    public void getOilTankList(String sellerId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId", sellerId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_OIL_TANK_LIST, requestParams, callback);
    }

    /**
     * 获取油枪列表
     *
     * @param machineId 油机id
     * @param callback
     */
    public void getOilGunList(String machineId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("machineId", machineId == null ? "" : machineId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_OIL_GUN_LIST, requestParams, callback);
    }

    /**
     * 获取油枪详情
     *
     * @param gunId    油枪id
     * @param callback
     */
    public void getOilGunDetail(String gunId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("gunId", gunId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_OIL_GUN_DETAIL, requestParams, callback);
    }

    /**
     * 添加油枪
     *
     * @param sellerId   油站id
     * @param machineId  油机id
     * @param potId      油罐id
     * @param num        油枪编号
     * @param name       油枪名称
     * @param initAmount 初始表数（毫升）
     * @param typeId     油品id
     * @param portNum    串口号
     * @param callback
     */
    public void addOilGunInfo(String sellerId, String machineId, String potId, String num, String name, String initAmount, String portNum, String typeId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId", sellerId);
        requestParams.put("machineId", machineId);
        requestParams.put("potId", potId);
        requestParams.put("num", num);
        requestParams.put("name", name);
        requestParams.put("initAmount", initAmount);
        requestParams.put("portNum", portNum);
        requestParams.put("typeId", typeId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_ADD_OIL_GUN_INFO, requestParams, callback);
    }

    /**
     * 修改油枪 信息
     *
     * @param gunId      油枪id
     * @param machineId  油机id
     * @param potId      油罐id
     * @param num        油枪编号
     * @param name       油枪名称
     * @param initAmount 初始表数（毫升）
     * @param portNum    串口号
     * @param typeId     油品id
     * @param callback
     */
    public void modifyOilGunInfo(String gunId, String machineId, String potId, String num, String name, String initAmount, String portNum, String typeId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("gunId", gunId);
        requestParams.put("machineId", machineId);
        requestParams.put("potId", potId);
        requestParams.put("num", num);
        requestParams.put("name", name);
        requestParams.put("initAmount", initAmount);
        requestParams.put("portNum", portNum);
        requestParams.put("typeId", typeId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_MODIFY_OIL_GUN_INFO, requestParams, callback);
    }

    /**
     * 删除油枪
     *
     * @param gunId
     * @param callback
     */
    public void delGun(String gunId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("gunId", gunId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_DEL_OIL_GUN, requestParams, callback);
    }

    /**
     * 初始化终端完毕，调用获取token令牌，进入主界面
     *
     * @param sellerId   油站id
     * @param machineId  油机id
     * @param machineNum 油机编号
     * @param deviceId   设备id唯一识别码
     * @param callback
     */
    public void getToken(String sellerId, String machineId, String machineNum, String deviceId, Callback callback) {
        requestParams = new HashMap<>();
        requestParams.put("sellerId", sellerId);
        requestParams.put("machineId", machineId);
        requestParams.put("machineNum", machineNum);
        requestParams.put("deviceId", deviceId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_GET_TOKEN, requestParams, callback);
    }

    /**
     * 获取首页的油枪信息
     *
     * @param token
     * @param machineId
     * @param callback
     */
    public void getTopGunsInfo(String token, String machineId, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getGunInfoRequestJson_1000(machineId));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 获取油卡信息
     *
     * @param token
     * @param gunId
     * @param cardNum
     * @param encryCarNum
     * @param callback
     */
    public void getCardInfo(String token, String machineId, String gunId, String cardNum, String encryCarNum, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getCardInfoRequest_1001(machineId, gunId, cardNum, encryCarNum));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 获取预约订单信息
     *
     * @param token
     * @param sellerId
     * @param gunId
     * @param orderNum
     * @param callback
     */
    public void getPreOrderInfo(String token, String sellerId, String gunId, String orderNum, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getPreOrderInfoRequest_1002(sellerId, gunId, orderNum));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 更新油枪状态
     *
     * @param token
     * @param gunId
     * @param state
     * @param callback
     */
    public void updateGunState(String token, String gunId, String state, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getUpdateGunStateRequest_1003(gunId, state));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 检测版本更新
     *
     * @param token
     * @param machineId
     * @param callback
     */
    public void checkeAPKVersion(String token, String machineId, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getCheckVersionRequest_1004(machineId));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);

    }

    /**
     * 提交订单
     *
     * @param token
     * @param amount         实付总金额=油品促销价*加油数量-优惠金额
     * @param machineNum     //油机编号
     * @param discountPrice  优惠总金额
     * @param orderNum       订单编号，终端生成
     * @param oilPotId       v油罐id
     * @param oilGunNum      油枪编号
     * @param oilGunId       油枪id
     * @param sellerId
     * @param machineId
     * @param oilCardId
     * @param price          油品单价
     * @param mlAmount       /加油数量（毫升）
     * @param oilTypeName
     * @param oilTypeId
     * @param cuPrice        促销单价
     * @param oilPotNum      油罐编号
     * @param isNormal       1正常 2异常
     * @param machineFlowNum
     * @param createD        加油时间
     * @param callback
     */
    public void createNewOrder(String token, String amount, String machineNum, String discountPrice, String orderNum,
                               String oilPotId, String oilGunNum, String oilGunId, String sellerId, String machineId,
                               String oilCardId, String price, String mlAmount, String oilTypeName, String oilTypeId,
                               String cuPrice, String oilPotNum, String isNormal, String machineFlowNum, String createD, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getCreteOrder_Request_1005(token, amount, machineNum,
                discountPrice, orderNum, oilPotId, oilGunNum, oilGunId, sellerId, machineId, oilCardId, price, mlAmount,
                oilTypeName, oilTypeId, cuPrice, oilPotNum, isNormal, machineFlowNum, createD));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 提交 预约订单
     *
     * @param token
     * @param orderNum
     * @param oilGunId
     * @param machineId
     * @param oilPotId
     * @param oilGunNum
     * @param oilTypeId
     * @param usePrice
     * @param price
     * @param mlAmount
     * @param machineNum
     * @param oilTypeName
     * @param cuPrice
     * @param oilPotNum
     * @param machineFlowNum //流水号
     * @param isNormal       //1正常订单 2异常订单
     * @param usedDStr       加油时间
     * @param callback
     */
    public void caretePreOrder(String token, String orderNum, String oilGunId, String machineId,
                               String oilPotId, String oilGunNum, String oilTypeId,
                               String usePrice, String price, String mlAmount, String machineNum,
                               String oilTypeName, String cuPrice, String oilPotNum, String machineFlowNum, String isNormal, String usedDStr,
                               Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getCreatePreOrder_Request_1009(token, orderNum, oilGunId, machineId,
                oilPotId, oilGunNum, oilTypeId, usePrice, price, mlAmount, machineNum, oilTypeName, cuPrice, oilPotNum, machineFlowNum, isNormal, usedDStr));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);

    }

    /**
     * 4.8	油卡加油订单，查询接口
     *
     * @param token
     * @param machineId
     * @param orderNum
     * @param callback
     */
    public void getCardFuelingOrder(String token, String machineId, String orderNum, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getCardFuelingOrder_Request_1010(token, machineId, orderNum));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);

    }

    /**
     * 现金支付
     *
     * @param token
     * @param machineId
     * @param orderNum
     * @param callback
     */
    public void payByCash(String token, String machineId, String orderNum, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).payCash_Request_1011(token, machineId, orderNum));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 油卡支付
     *
     * @param token
     * @param machineId
     * @param orderNum
     * @param callback
     */
    public void payByCard(String token, String machineId, String orderNum, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).payCard_Request_1012(token, machineId, orderNum));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 获取广告列表
     *
     * @param token
     * @param machineId
     * @param callback
     */
    public void getAdList(String token, String machineId, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getAdList_1013(token, machineId));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);

    }

    /**
     * 获取二维码(支付宝)
     *
     * @param token
     * @param orderNum
     * @param machineId
     * @param callback
     */
    @Deprecated
    public void getErWeiMa(String token, String orderNum, String sellerId, String machineId, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).getErWeiMa_1006(token, orderNum, sellerId, machineId));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 可以获取支付宝 或 微信 的支付二维码
     * @param payMode 支付方式 Common.ACT_GET_ZFB_ERWEIMA_1006    /Common.ACT_GET_WX_ERWEIMA_1005
     * @param token
     * @param orderNum
     * @param sellerId
     * @param machineId
     * @param callback
     */
    public void getErWeiMa(String payMode, String token, String orderNum, String sellerId, String machineId, Callback callback) {
        requestParams = new HashMap<>();
        String json = "";
        if (Common.PAY_MODE_ZHIFUBAO.equals(payMode)) {
            json = new Gson().toJson(WsReqParam.getInstance(token).getZFBErWeiMa_1006(token, orderNum, sellerId, machineId));

        } else if (Common.PAY_MODE_WEIXIN.equals(payMode)) {
            json = new Gson().toJson(WsReqParam.getInstance(token).getWXErWeiMa_1015(token, orderNum, sellerId, machineId));

        }
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 查询订单支付状态
     *
     * @param token
     * @param machineId
     * @param orderNum
     * @param callback
     */
    public void getOrderPayState(String token, String machineId, String orderNum, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).checkOrderPayState_1010(token, orderNum, machineId));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);
    }

    /**
     * 解锁订单
     *
     * @param token
     * @param machineId
     * @param orderNum
     * @param callback
     */
    public void unLockOrder(String token, String machineId, String orderNum, Callback callback) {
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).unLockOrder_1014(token, orderNum, machineId));
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);

    }

    /**
     * 查询订单时候的 登录
     * @param userName
     * @param pwd
     * @param sellerId
     * @param callback
     */
    public void fillerLogin(String userName,String pwd,String sellerId,Callback callback){
        requestParams = new HashMap<>();
        requestParams.put("userName", userName);
        requestParams.put("pwd", pwd);
        requestParams.put("sellerId", sellerId);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL + Common.HTTP_FILLER_LOGIN, requestParams, callback);
    }

    /**
     * 更新 工作模式  自助非自助
     * @param token
     * @param gunId
     * @param isSelf
     * @param callback
     */
    public void updateWorkMode(String token,String gunId,boolean isSelf,Callback callback){
        requestParams = new HashMap<>();
        String json = new Gson().toJson(WsReqParam.getInstance(token).updateWorkMode_1018(token, gunId, isSelf ? "2" : "1" ));//工作模式 1非自助 2自助
        requestParams.put("inputJson", json);
        OkhttpUtils.getInstance().postDataAsynToNet(Common.HTTP_BASE_URL_API, requestParams, callback);

    }

}